package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insert(dish);


        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断flavor是为空

        if (flavors == null && flavors.size() == 0) {
            return;
        }

//返回菜品主键
        Long dishId = dish.getId();
        //为每个flavor赋ID值
        flavors.forEach(dishFlavor ->
                dishFlavor.setDishId(dishId)
        );

        dishFlavorMapper.insertBatch(flavors);

    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    /**
     * 批量删除菜品
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否在售
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if (dish.getStatus() == 1) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断菜品是否在套餐中
        List<Long> setmealDishListBySetmealId = setmealDishMapper.getSetmealDishListByDishIds(ids);

        if (setmealDishListBySetmealId != null && setmealDishListBySetmealId.size() > 0){
            new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品数据

        dishMapper.deleteDishBatch(ids);

        //删除菜品口味数据
        dishFlavorMapper.deleteDishFlavorByDishIdBatch(ids);

    }

    @Override
    public DishVO getDishById(Long id) {
        Dish dish = dishMapper.getDishById(id);

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        //查询菜品口味数据
        List<DishFlavor> dishFlavors = dishFlavorMapper.getDishFlavorById(id);

        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        //先删除口味 数据
        dishFlavorMapper.deleteDishFlavorByDishIdBatch(Collections.singletonList(dishDTO.getId()));

        //添加口味数据
        if (dishDTO.getFlavors() == null && dishDTO.getFlavors().size() == 0) {
            return;
        }

        //为每个flavor赋ID值
        dishDTO.getFlavors().forEach(dishFlavor ->
                dishFlavor.setDishId(dishDTO.getId())
        );

        dishFlavorMapper.insertBatch(dishDTO.getFlavors());

    }

}
